package cn.edu.zafu.bigdata.x.zcy_0217;
import java.util.Random;
import java.util.Scanner;

public class GuessNumberGame_0217_zcy {
    private final int numberToGuess;
    private final int maxGuesses;
    private int currentGuesses;

    public GuessNumberGame_0217_zcy(int maxGuesses) {
        this.maxGuesses = maxGuesses;
        this.currentGuesses = 0;
        this.numberToGuess = new Random().nextInt(100) + 1; // 生成一个1到100之间的随机数
    }

    public void startGame() {
        Scanner scanner = new Scanner(System.in);
        boolean hasGuessedCorrectly = false;

        System.out.println("欢迎来到猜数字游戏！你有 " + maxGuesses + " 次机会猜出一个1到100之间的数字。");

        while (currentGuesses < maxGuesses && !hasGuessedCorrectly) {
            System.out.print("猜一猜数字：");
            int guess = scanner.nextInt();
            currentGuesses++;

            if (guess == numberToGuess) {
                hasGuessedCorrectly = true;
                System.out.println("恭喜你！猜对了，数字是 " + numberToGuess);
            } else if (guess < numberToGuess) {
                System.out.println("太小了，再试试！");
            } else if (guess > numberToGuess) {
                System.out.println("太大了，再试试！");
            }
        }

        if (!hasGuessedCorrectly) {
            System.out.println("很遗憾，你已经用完了所有的猜测次数。游戏结束。正确答案是：" + numberToGuess);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        int maxGuesses = 5; // 可以限定最大次数
        GuessNumberGame_0217_zcy game = new GuessNumberGame_0217_zcy(maxGuesses);
        game.startGame();

        // 多轮游戏
        System.out.println("想再玩一次吗？(yes/no)");
        Scanner scanner = new Scanner(System.in);
        String playAgain = scanner.nextLine();
        scanner.close();

        if ("yes".equalsIgnoreCase(playAgain)) {
            game = new GuessNumberGame_0217_zcy(maxGuesses);
            game.startGame();
        }
    }
}